package cn.tedu.hospitalqd.service.impl;

import cn.tedu.hospitalqd.ex.ServiceException;
import cn.tedu.hospitalqd.mapper.DoctorMapper;
import cn.tedu.hospitalqd.pojo.dto.DoctorAddNewDTO;
import cn.tedu.hospitalqd.pojo.dto.DoctorUpdateDTO;
import cn.tedu.hospitalqd.pojo.entity.Doctor;
import cn.tedu.hospitalqd.pojo.vo.DoctorListItemVO;
import cn.tedu.hospitalqd.pojo.vo.DoctorStandardVO;
import cn.tedu.hospitalqd.service.DoctorService;
import cn.tedu.hospitalqd.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
   private DoctorMapper doctorMapper;
   @Override
   public void addNew(DoctorAddNewDTO dto){
        String dNumber= dto.getDNumber();
        int count=doctorMapper.countBydNumber(dNumber);
        if (count>0){
          String message="添加医生失败,医生号码已被占用";
         throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
       Doctor doctor=new Doctor();
       BeanUtils.copyProperties(dto,doctor);
       int rows = doctorMapper.insert(doctor);
       if (rows!=1){
           String message = "添加医生失败，服务器忙，请稍后再尝试！";
           throw new ServiceException(ServiceCode.ERR_INSERT, message);
       }
   }

    @Override
    public void delete(Long id) {
        DoctorStandardVO queryResult = doctorMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "删除医生详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        int rows = doctorMapper.deleteById(id);
        if (rows!=1){
            String message = "删除医生失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }


    }

    @Override
    public void updateInfoById(Long id, DoctorUpdateDTO updateDTO) {
        DoctorStandardVO queryResult = doctorMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "修改医生详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        Doctor doctor=new Doctor();
        BeanUtils.copyProperties(updateDTO,doctor);
        doctor.setId(id);
        log.debug("即将执行修改数据，参数：{}", doctor);
        int rows = doctorMapper.update(doctor);
        if (rows != 1) {
            String message = "修改医生详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }





    }

    @Override
    public DoctorStandardVO getStandardById(Long id) {
        DoctorStandardVO queryResult = doctorMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "获取医生详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        return queryResult;

    }

    @Override
    public List<DoctorListItemVO> list() {
        List<DoctorListItemVO> list=doctorMapper.list();
        return list;
    }
}
